package com.zx.base.ctrl;

import android.app.Activity;
import android.content.Intent;
import android.content.IntentFilter;
import android.content.SharedPreferences;
import android.net.ConnectivityManager;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;

import com.zx.base.util.ConnectionChangeReceiver;
import com.zx.tradeclient.R;
import com.zx.tradeclient.app.TradeApp;

public abstract class BaseActivity extends Activity {



	private ConnectionChangeReceiver receiver;
	private BaseActivity requestSubActivity;
	public Button	header_left,header_right;
	public TextView header_title;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
	}

	@Override
	protected void onResume() {
		super.onResume();
		IntentFilter filter = new IntentFilter(ConnectivityManager.CONNECTIVITY_ACTION);
        receiver = new ConnectionChangeReceiver();
        this.registerReceiver(receiver, filter);
	}

	@Override
	protected void onPause() {
		super.onPause();
		if (receiver != null) {
			this.unregisterReceiver(receiver);
		}
	}

	protected SharedPreferences getPreferences() {
		return getSharedPreferences(Constant.PREFERENCE_NAME,Activity.MODE_PRIVATE);
	}

	protected TradeApp getMyApplication() {
		return (TradeApp) getApplication();
	}
	
	public void onDestroy() {
		super.onDestroy();
	}

	public BaseActivity getRequestSubActivity() {
		return requestSubActivity;
	}

	public void setRequestSubActivity(BaseActivity requestSubActivity) {
		this.requestSubActivity = requestSubActivity;
	}

	private Class<?> getTargetClass(Intent intent){
		Class<?> clazz = null;
		try {
			if(intent.getComponent() != null)
			clazz = Class.forName(intent.getComponent().getClassName());
		} catch (ClassNotFoundException e) {
			e.printStackTrace();
		}
		return clazz;
	}
	
	// 重写了startActivity()方法，
	// 这样调用该方法时会根据目标Activity是否是子Activity而调用不同的方法
	@Override
	public void startActivity(Intent intent) {
		if( getTargetClass(intent) != null && BaseActivity.class.isAssignableFrom(getTargetClass(intent)) ){
			if(this.getParent() instanceof BaseGroup){
				intent.putExtra("fromSubActivity", getClass().getName());
				((BaseGroup)this.getParent()).launchNewActivity(intent);
			}
		}else{
			super.startActivity(intent);
		}
	}

	// 重写了startActivityForResult()方法，
	// 这样调用该方法时会根据目标Activity是否是子Activity而调用不同的方法
	@Override
	public void startActivityForResult(Intent intent, int requestCode) {
		if( getTargetClass(intent) != null && BaseActivity.class.isAssignableFrom(getTargetClass(intent)) ){
			if(this.getParent() instanceof BaseGroup){
				intent.putExtra("fromSubActivity", getClass().getName());
				((BaseGroup) this.getParent())
						.launchNewActivityForResult(this, intent, requestCode);
			}
		}else{
			super.startActivityForResult(intent, requestCode);
		}
	}
		
	/** 调用此方法来返回上一个界面 */
	public void goback() {
		Class<?> clazz = null;
		try {
			clazz = Class.forName(getIntent().getStringExtra("fromSubActivity"));
		} catch (ClassNotFoundException e) {
			e.printStackTrace();
		}
		Intent intent = new Intent(this,clazz);
		((BaseGroup)this.getParent()).launchActivity(intent);
	}
	
	/** 调用此方法来返回上一个界面并返回数据 */
	public void gobackWithResult(int resultCode, Intent data) {
		Class<?> clazz = null;
		try {
			clazz = Class.forName(getIntent().getStringExtra("fromSubActivity"));
		} catch (ClassNotFoundException e) {
			e.printStackTrace();
		}
		data.setClass(this, clazz);
		if( requestSubActivity != null){
			requestSubActivity.onActivityResult(
					data.getIntExtra("requestCode", 0), resultCode, data);
		}
		((BaseGroup)this.getParent()).launchActivity(data);	
	}

	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event) {
		if (keyCode == KeyEvent.KEYCODE_BACK) {
			goback();
			return true;
		}
		return super.onKeyDown(keyCode, event);
	}
	public void setHeader(int type){
		header_left=(Button)findViewById(R.id.header_left);
		header_right=(Button)findViewById(R.id.header_right);
		header_title = (TextView)findViewById(R.id.header_title);
		
		switch (type) {
		case 5:
			header_left.setVisibility(View.GONE);
			header_right.setVisibility(View.GONE);
			break;
		case 0:
			header_left.setVisibility(View.VISIBLE);
			header_right.setVisibility(View.GONE);
			header_left.setText("返回");
			header_left.setOnClickListener(new OnClickListener() {
				
				@Override
				public void onClick(View v) {
					// TODO Auto-generated method stub
					goback();
				}
			});

			break;
		case 1:
			header_left.setVisibility(View.VISIBLE);
			header_right.setVisibility(View.VISIBLE);
			header_left.setText("返回");
			header_right.setText("提交");
			header_left.setOnClickListener(new OnClickListener() {
				
				@Override
				public void onClick(View v) {
					// TODO Auto-generated method stub
					goback();
				}
			});
			break;
		case 2:
			header_left.setVisibility(View.VISIBLE);
			header_right.setVisibility(View.VISIBLE);
			header_left.setText("返回");
			header_right.setText("添加");
			header_left.setOnClickListener(new OnClickListener() {
				
				@Override
				public void onClick(View v) {
					// TODO Auto-generated method stub
					goback();
				}
			});
			break;
		case 3:
			header_left.setVisibility(View.VISIBLE);
			header_right.setVisibility(View.VISIBLE);
			header_left.setText("返回");
			header_right.setText("评论详情");
			header_left.setOnClickListener(new OnClickListener() {
				
				@Override
				public void onClick(View v) {
					// TODO Auto-generated method stub
					goback();
				}
			});
		
			break;
		default:
			break;
		}
	}
	
}
